package cn.crc.web.tool;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.math.BigDecimal;
import java.util.*;

/**
 * 基础使用方法_解析读写XML_25.4.16_liuhz
 */
public class TestHutool {
    public static void main(String[] args) {
        TestHutool.testBase1();//随机数..加减
        TestHutool.testMap2Bean();
        TestHutool.testJson1();
        TestHutool.testJob();//定时任务
        //TestHutool.testExcel();//需要引入poi
        TestHutool.test日期时间1();
        TestHutool.test日期时间2();
        //TestEasyXml.testBaseXml();//解析XML
    }

    public static void testBase1(){
        String randomStr = RandomUtil.randomString(8); // 生成指定长度的随机字符串
        BigDecimal num1 = new BigDecimal("10.5");
        BigDecimal num2 = new BigDecimal("5.2");
        BigDecimal result = NumberUtil.add(num1, num2); // 使用BigDecimal进行精确运算
        System.out.println("随机数："+randomStr+";数字："+result);
    }
    public static void testMap2Bean(){
        Map<String, Object> map = new HashMap<>();
        map.put("username", "大钟寺");
        map.put("age", 30);
        TestUserVo user1 = BeanUtil.mapToBean(map, TestUserVo.class, true); // 将Map转换为Java Bean
        TestUserVo user2 = BeanUtil.toBeanIgnoreCase(map, TestUserVo.class, false);
        Map mapBean= BeanUtil.beanToMap(user1);
        System.out.println("Map2Bean\t"+user1);
        System.out.println("Map2Bean\t"+user2);
        System.out.println("bean2map\t"+mapBean);
    }
    public static void testJson1(){
        String jsonString = "{\"name\":\"高速机车\",\"age\":30}";
        JSONObject jsonObject = JSONUtil.parseObj(jsonString); // 将JSON字符串转换为JSON对象
        System.out.println("简单json\t"+jsonObject);
        String json1= JSONUtil.toJsonStr(TestVipVo.getVipTest());
        TestVipVo vipVo= JSONUtil.toBean(json1, TestVipVo.class);
        System.out.println("复杂json\t"+json1+"\t"+vipVo);
        String json2= JSONUtil.toJsonStr(TestVipVo.getUserTest());
        List<TestUserVo> listUser= JSONUtil.toList(json2, TestUserVo.class);
        System.out.println("list-json\t"+json2+"\t"+listUser);
    }
    public static void testJob(){
        String cron1 = "0 0/1 * * * ?";
        String cron2 = "0/5 * * * * ?";
        CronUtil.schedule("testJob1", cron1, () -> System.out.println("执行任务"+DateUtil.now()) );
        //CronUtil.schedule("testJob2", cron1, () -> testJson1()); // 解析Cron表达式并执行任务
        CronUtil.schedule("testJob6", cron2, () -> {
            testBase1();
        });
        CronUtil.setMatchSecond(true);
        CronUtil.start();
    }
    public static void testExcel(){//需要引入poi
        String filepath= "E:\\在线率详情_2025-04-07.xlsx";
        ExcelReader reader = ExcelUtil.getReader(filepath); // 读取Excel文件
        List<List<Object>> dataList = reader.read(); // 读取数据
        System.out.println(dataList);
    }

    public static void test日期时间1(){
        String dateStr = "2025-04-16 15:02:03";
        Date date = DateUtil.parse(dateStr);// 解析日期字符串为Date对象

        // 格式化日期对象
        String format = DateUtil.format(date, "yyyy/MM/dd"); // 结果: 2025/04/16
        String formatDate = DateUtil.formatDate(date); // 结果: 2025-04-16 15:02:03
        String formatDateTime = DateUtil.formatDateTime(date); // 结果: 15:02:03
        String formatTime = DateUtil.formatTime(date); // 结果: 15:02:03
        System.out.println("格式化日期："+format+","+formatDate+","+formatDateTime+","+formatTime);

        // 计算两个日期之间的差异
        Date startDate = DateUtil.parse("2025-04-16 15:00:00");
        Date endDate = DateUtil.parse("2025-04-18 16:10:00");
        long week = DateUtil.between(startDate, endDate, DateUnit.WEEK); // 相差周数
        long day = DateUtil.between(startDate, endDate, DateUnit.DAY); // 相差天数
        long hour = DateUtil.between(startDate, endDate, DateUnit.HOUR); // 相差小时数
        System.out.println("计算日期差异："+week+","+day+","+hour);
    }
    public static void test日期时间2(){
        DateTime dateTime= DateUtil.date();
        String format1= DateUtil.format(dateTime, DatePattern.NORM_DATETIME_FORMAT);
        String format2= DateUtil.format(dateTime, DatePattern.NORM_DATETIME_MINUTE_FORMAT);
        String format3= DateUtil.format(dateTime, DatePattern.NORM_DATE_FORMATTER);
        String format4= DateUtil.format(dateTime, DatePattern.NORM_DATETIME_MS_FORMATTER);
        System.out.println("日期格式NORM："+dateTime+".."+format1+","+format2+","+format3+","+format4);
        String formatA= DateUtil.format(dateTime, DatePattern.PURE_DATE_FORMATTER);
        String formatB= DateUtil.format(dateTime, DatePattern.PURE_DATETIME_FORMATTER);
        String formatC= DateUtil.format(dateTime, DatePattern.PURE_DATETIME_MS_FORMATTER);
        System.out.println("日期格式PURE："+dateTime+".."+formatA+","+formatB+","+formatC);

        DateTime offDateDay= DateUtil.offsetDay(dateTime, 1);
        DateTime offDateHour= DateUtil.offsetHour(dateTime, 1);
        DateTime offDateMonth= DateUtil.offsetMonth(dateTime, 1);
        System.out.println("日期加减："+dateTime+".."+offDateDay+","+offDateHour+","+offDateMonth);
    }

    public static void testBaseXml(){
        String xmlstr="<list id='1'><person key='one'><user num='1-1'>张三</user><user num='2-1'>李四</user></person></list>";
        Document document = XmlUtil.readXML(xmlstr);//读取xml
        Element rootEle = XmlUtil.getRootElement(document);//获取根节点element
        String rootID = rootEle.getAttribute("id");//获取根节点element对应属性
        Element itemEle = XmlUtil.getElement(rootEle, "person");//获取指定名称节点element
        String itemKEY = itemEle.getAttribute("key");//获取指定名称节点element对应属性

        List<Element> sonList= XmlUtil.getElements(itemEle, "user");//集合式节点
        List<String> sonText = new ArrayList<>();
        List<String> sonID = new ArrayList<>();
        for(Element element : sonList){
            String text = element.getTextContent();//属性
            sonText.add(text);
            String sonStr = element.getAttribute( "num");//文本
            sonID.add(sonStr);
        }
        System.out.println(rootID+","+itemKEY);
        System.out.println(sonText);
        System.out.println(sonID);
    }

    public static void test11(){//无效
        TestUserVo userVo = new TestUserVo();
        userVo.setUsername("张三");
        userVo.setTelephone("13011008899");
        Document document = XmlUtil.beanToXml(userVo);// 将Java对象转换为XML字符串
        System.out.println(document);
    }
    public static void test22(){//无效
        String xmlstr="<list id='1'><person key='one'><user num='1-1'>张三</user><user num='2-1'>李四</user></person></list>";
        Document document = XmlUtil.beanToXml(xmlstr);// 将XML字符串转换为Java对象
        System.out.println(document);
    }

}
